package com.jt.sys.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.jt.common.annotation.RequestLog;
import com.jt.common.exception.ServiceException;
import com.jt.common.vo.Node;
import com.jt.sys.dao.SysDeptDao;
import com.jt.sys.entity.SysDept;
import com.jt.sys.service.SysDeptService;
@Service
@Transactional
public class SysDeptServiceImpl implements SysDeptService {
	@Autowired
	private SysDeptDao sysDeptDao;
	@Override
	@RequestLog("查看组织信息")
	public List<SysDept> findObjects() {
		
		return sysDeptDao.findObjects();
	}
	@Override
	@RequestLog("删除组织信息")
	public int deleteObjectById(Integer id) {
		if(id==null||id<=0)
			throw new IllegalArgumentException("请先选择要删除的项目");
		int count = sysDeptDao.getChildCount(id);
		if(count>0)
			throw new ServiceException("请现删除子元素");
		int rows = sysDeptDao.deleteObjectById(id);
		if(rows!=1)
			throw new ServiceException("删除失败");
		return rows;
	}
	@Override
	@RequestLog("添加组织信息")
	public int saveObject(SysDept entity) {
		if(entity==null)
			throw new ServiceException("保存对象不能为空");
		if(StringUtils.isEmpty(entity.getName()))
			throw new ServiceException("部门不能为空");
		int rows;
			//2.保存数据
		try{
			rows=sysDeptDao.insertObject(entity);
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException("保存失败");
		}
			//3.返回数据
		return rows;
	}
	@Override
	public List<Node> findZTreeDeptNodes() {
		
		return sysDeptDao.findZTreeDeptNodes();
	}
	@Override
	@RequestLog("更新组织信息")
	public int updateObject(SysDept entity) {
		//1.合法验证
		if(entity==null)
			throw new ServiceException("保存对象不能为空");
		if(StringUtils.isEmpty(entity.getName()))
			throw new ServiceException("部门不能为空");
		int rows;
		//2.更新数据
		try{
			rows=sysDeptDao.updateObject(entity);
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException("更新失败");
		}
		//3.返回数据
		return rows;
	}

}
